﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ProcessFile
{
   public class AD14File
    {
        public string _filePath;
       public string _line;
       
       public AD14File(string filePath)
       {
           _filePath = filePath;
           _line = Common.ReadFileTextOneLine(filePath);
       }
       public  string Header
       {
           get
           {
               int indexEnd = _line.IndexOf("LAYER_V8_2LAYERID");
               string context = _line.Substring(0, indexEnd );
               return context;
           }
       }
       public string Footer
       {
           get
           {
               int indexEnd = _line.IndexOf(AD14LayerId.LAYER_V8_Bottom_Overlay.ToString());
               int freIndex = _line.Substring(0, indexEnd).LastIndexOf("LAYER_");
               string context = _line.Substring(freIndex);
               return context;
           }
       }
       public  string SubContent
       {
           get
           {
               int indexStart = _line.IndexOf("DISPLAYUNIT=1");
               int indexEnd = _line.IndexOf("LAYER_V8_0LAYERID");
               string context = _line.Substring(indexStart, indexEnd - indexStart);
               return context;
           }
       }
       private  string LayerContent
       {
           get {
               int indexStart = _line.IndexOf("NAME=Top Overlay");
               int indexEnd = _line.IndexOf("NAME=Bottom Overlay");
               string context = _line.Substring(indexStart, indexEnd - indexStart);
               return context;
           }
       }
       public string Unit
       {
           get
           {
               if (LayerContent.Contains("mil"))
               {
                   return "mil";
               }
               return "um";
           
           }
       }

       public  List< SubStackItem> SubStackItemContent
       {
           get
           {
               string pattern = "ID={";
               Regex regex = new Regex(pattern);
               string[] lines = regex.Split(SubContent);
               List<SubStackItem> subStack = new List<SubStackItem>();
               foreach (string line in lines)
               {
                   string name = ConvertAD144StkRow.GetLayerName(line);
                   if (!string.IsNullOrEmpty(name))
                   {
                       SubStackItem item = new SubStackItem();
                       item.SubName = name;
                       item.SubStackId = SubStackItem.GetLayerID(line);
                       subStack.Add(item);
                   }
                  
               }
               return subStack;

           }
       }
       public  List<string> LayerConten()
       {
        List<string> listContent=new List<string>(); 
               int layerIndex = 2;
               while (_line.IndexOf("LAYER_V8_" + layerIndex) > 0)
               {
                   int startIndex = _line.IndexOf("LAYER_V8_" + layerIndex);
                   int nextIndex = _line.IndexOf("LAYER_V8_" + (layerIndex + 1));
                   if (nextIndex > startIndex)
                   {
                       string layerContent = _line.Substring(startIndex, nextIndex - startIndex);
                      listContent.Add(layerContent);
                   }
                   else
                   {
                       string layerContent = _line.Substring(startIndex);
                       listContent.Add(layerContent);
                   }
                   layerIndex++;
               }
           return listContent;
       }
       public string[] LayerItems
       {
           get
           {
               string pattern = "ID={";
               Regex regex = new Regex(pattern);
               string[] lines= regex.Split(LayerContent);

               return lines;

           }
       }
    }
}
